package com.caojing.syby.httputils;


import android.util.Log;

import com.caojing.syby.bean.brand.BrandBean;
import com.caojing.syby.bean.home.HomeGvBean;
import com.caojing.syby.bean.home.HomeTodayBean;
import com.caojing.syby.bean.home.HomeVpBean;
import com.caojing.syby.bean.tenbuy.ShareGvBean;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/*访问网络的工具，把retrofit封装到里面，方便访问网络
* 小组成员要把所有访问网络的方法都写到这里，下面的注释有模板*/
public class HttpUtils {

    private static HttpUtils instance;

    private HttpUtils() {
    }

    public static synchronized HttpUtils getInstance() {
        if (instance == null) {
            instance = new HttpUtils();
        }
        return instance;
    }


    /*回掉接口*/
    public interface HttpCallBack<T> {
        void onSucess(T t);

        void onFail();
    }
    /*加载首页vviewpager数据的方法*/
    public void getHomeVpBean(final HttpCallBack<HomeVpBean> homeVpBeanHttpCallBack){
        Call<HomeVpBean> homeVpBeanCall = RetrofitHelper
                .getInstance()
                .createRetrofitService(ITenYuanBuyService.class)
                .getHomeVpBean();
        homeVpBeanCall.enqueue(new Callback<HomeVpBean>() {
            @Override
            public void onResponse(Call<HomeVpBean> call, Response<HomeVpBean> response) {
                HomeVpBean homeVpBean = response.body();
                homeVpBeanHttpCallBack.onSucess(homeVpBean);
            }

            @Override
            public void onFailure(Call<HomeVpBean> call, Throwable t) {
                homeVpBeanHttpCallBack.onFail();
            }
        });
    }
    /*加载首页中间视图的方法*/
    public void getHomeTodayBean(final HttpCallBack<List<HomeTodayBean>> listHttpCallBack){
        Call<List<HomeTodayBean>> listCall = RetrofitHelper
                .getInstance()
                .createRetrofitService(ITenYuanBuyService.class)
                .getHomeTodayBean();
        listCall.enqueue(new Callback<List<HomeTodayBean>>() {
            @Override
            public void onResponse(Call<List<HomeTodayBean>> call, Response<List<HomeTodayBean>> response) {
                List<HomeTodayBean> list = response.body();
                listHttpCallBack.onSucess(list);
            }

            @Override
            public void onFailure(Call<List<HomeTodayBean>> call, Throwable t) {
                listHttpCallBack.onFail();
            }
        });
    }
    /*加载首页gridview数据的方法*/
    public void getHomeGvBean(HashMap<String,String> params, final HttpCallBack<HomeGvBean> homeGvBeanHttpCallBack){
        Call<HomeGvBean> homeGvBeanCall = RetrofitHelper
                .getInstance()
                .createRetrofitService(ITenYuanBuyService.class)
                .getHomeGvBean(params);
        homeGvBeanCall.enqueue(new Callback<HomeGvBean>() {
            @Override
            public void onResponse(Call<HomeGvBean> call, Response<HomeGvBean> response) {
                HomeGvBean homeGvBean = response.body();
                //Log.i("TAG","httputils成功"+homeGvBean.getRows().size());
                homeGvBeanHttpCallBack.onSucess(homeGvBean);
            }
            @Override
            public void onFailure(Call<HomeGvBean> call, Throwable t) {
                Log.i("TAG","异常"+t.getMessage());
                homeGvBeanHttpCallBack.onFail();
            }
        });
    }

//    这是十元购部分的方法
    public void getVerticalSharegvData(Map<String,String>params, Callback<ShareGvBean>callback){
             Call<ShareGvBean>shareGvBeanCall=RetrofitHelper
                     .getInstance()
                     .createRetrofitService(ITenYuanBuyService.class)
                     .getShareGvBean(params);

        shareGvBeanCall.enqueue(callback);


    }


    //加载品牌特卖的数据
    public void getBrandSale(HashMap<String,String> params, final HttpCallBack<BrandBean> brandBeanHttpCallBack){
        final Call<BrandBean> brandBeanCall=RetrofitHelper
                .getInstance()
                .createRetrofitService(ITenYuanBuyService.class)
                .getBrandChild(params);
        brandBeanCall.enqueue(new Callback<BrandBean>() {
            @Override
            public void onResponse(Call<BrandBean> call, Response<BrandBean> response) {
                //判断是否的到数据
                if (response.isSuccessful()){
                    BrandBean body = response.body();
                    brandBeanHttpCallBack.onSucess(body);
                }
            }

            //没有数据，或加载错误时显示
            @Override
            public void onFailure(Call<BrandBean> call, Throwable t) {
//                    Log.i("TAG",t.printStackTrace().to);
                brandBeanHttpCallBack.onFail();
            }
        });
    }



}

  /*  public void getHomeVpBean(final HttpCallBack<HomeVpBean> homeVpBeanHttpCallBack) {
        Call<HomeVpBean> homeVpBeanCall = RetrofitHelper
                .getInstance()
                .createRetrofitService(IHomeService.class)
                .getHomeVpBean();
        homeVpBeanCall.enqueue(new Callback<HomeVpBean>() {
            @Override
            public void onResponse(Call<HomeVpBean> call, Response<HomeVpBean> response) {
                HomeVpBean homeVpBean = response.body();
                homeVpBeanHttpCallBack.onSucess(homeVpBean);
            }
            @Override
            public void onFailure(Call<HomeVpBean> call, Throwable t) {
                homeVpBeanHttpCallBack.onFail();
            }
        });
    }
    public void getHomeLvBean(final HttpCallBack<HomeLvBean> homeLvBeanHttpCallBack){
        Call<HomeLvBean> homeLvBeanCall = RetrofitHelper
                .getInstance()
                .createRetrofitService(IHomeService.class)
                .getHomeLvBean();
        homeLvBeanCall.enqueue(new Callback<HomeLvBean>() {
            @Override
            public void onResponse(Call<HomeLvBean> call, Response<HomeLvBean> response) {
                HomeLvBean homeLvBean = response.body();
                homeLvBeanHttpCallBack.onSucess(homeLvBean);
            }
            @Override
            public void onFailure(Call<HomeLvBean> call, Throwable t) {
                homeLvBeanHttpCallBack.onFail();
            }
        });*/
   /* }*/

/*  @Override
            public void onResponse(Call<HotShowEntity> call, Response<HotShowEntity> response) {
                HotShowEntity entity = response.body();
                for (MovieEntity movieEntity : entity.getList()) {
                    Log.d("-----", "" + movieEntity.getName());
                }
                entityHttpCallBack.onSucess(response.body());
            }

            @Override
            public void onFailure(Call<HotShowEntity> call, Throwable t) {
                entityHttpCallBack.onFail();
            }*/

